package com.example.larou;

import cn.dev33.satoken.SaManager;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.endpoint.MessageProducerSupport;
import org.springframework.integration.handler.LoggingHandler;
import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory;
import org.springframework.integration.mqtt.core.MqttPahoClientFactory;
import org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter;
import org.springframework.integration.mqtt.support.DefaultPahoMessageConverter;

@SpringBootApplication
@MapperScan("com.example.larou.mapper")
public class LarouApplication {

    public static void main(String[] args) {
        SpringApplication.run(LarouApplication.class, args);
        System.out.println("启动成功，Sa-Token 配置如下：" + SaManager.getConfig());
    }

//    @Bean
//    public MqttPahoClientFactory mqttClientFactory(){
//        DefaultMqttPahoClientFactory defaultMqttPahoClientFactory=new DefaultMqttPahoClientFactory();
//        MqttConnectOptions options=new MqttConnectOptions();
//        options.setServerURIs(new String[] { "tcp://183.230.40.96:1883"});
//        options.setUserName("0P4O8xa922");
//        options.setPassword("version=2018-10-31&res=products%2F0P4O8xa922%2Fdevices%2Fc4&et=1786918400&method=md5&sign=bgodgUkxJDhCEv9HmNDz1A%3D%3D".toCharArray());
//        defaultMqttPahoClientFactory.setConnectionOptions(options);
//        return defaultMqttPahoClientFactory;
//    }
//
//    @Bean
//    public IntegrationFlow mqttInFlow() {
//        return IntegrationFlow.from(mqttInbound())
//                .transform(p -> p + ", received from MQTT")
//                .handle(logger())
//                .get();
//    }
//
//    private LoggingHandler logger() {
//        LoggingHandler loggingHandler = new LoggingHandler("INFO");
//        loggingHandler.setLoggerName("siSample");
//        return loggingHandler;
//    }
//
//    @Bean
//    public MessageProducerSupport mqttInbound() {
//        MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter("c4",
//                mqttClientFactory(), "$sys/0P4O8xa922/c4/thing/property/post/reply");
//        adapter.setCompletionTimeout(10000);
//        adapter.setConverter(new DefaultPahoMessageConverter());
//        adapter.setQos(1);
//        return adapter;
//    }

}
